// gulp 异步任务三种方式  

const fs = require('fs')

exports.callback = done =>{
    console.log('callback')
    done()
}

// 错误优先，在多个任务中， 有错误的时候， 就会阻止后面的程序进行  
exports.callback_error = done =>{
    console.log('callback error')
    done(new Error('task error'))
}

// 支持promise async...await 异步方法  
// 需要在node版本支持promise async...await 
exports.promise = () =>{
    console.log('promise')
    return Promise.resolve()
}

exports.promise_error = () =>{
    console.log('promise error')
    return Promise.reject(new Error('promose error task'))
}

const timeout = time =>{
    return new Promise(resolve=>{
        setTimeout(resolve, time)
    })
}

exports.async = async ()=>{
    await timeout(1000)
    console.log('async task')
}

// exports.stream = () =>{
//     const read = fs.createReadStream('yarn.lock')
//     const write = fs.createWriteStream('yarn.text')
//     read.pipe(write)
//     return read
// }

exports.stream = (done) =>{
    const read = fs.createReadStream('yarn.lock')
    const write = fs.createWriteStream('yarn.text')
    read.pipe(write)
    read.on('end', ()=>{
        done()
    })
}